﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="PayBase.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：PayBase.cs
// 创建时间：2015-01-08 11:56
// 修改时间：2015-01-08 11:56
// </summary>
// ---------------------------------------------------------------------------------------
#endregion
namespace Payment
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    using WebCommon;

    /// <summary>
    /// 支付父类
    /// </summary>
    public abstract class PaySubBase
    {


        /// <summary>
        /// 网关
        /// </summary>
        /// <returns></returns>
        public string Gateway { get; set; }

        ///// <summary>
        ///// 返回URL
        ///// </summary>
        ///// <returns></returns>
        //public string ReturnUrl { get; set; }

        ///// <summary>
        ///// 通知url
        ///// </summary>
        ///// <returns></returns>
        //public string NoticeUrl { get; set; }

        /// <summary>
        /// get或post
        /// </summary>
        /// <returns></returns>
        protected abstract string Method();

        /// <summary>
        /// The 发送的html form.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public virtual string Fasong()
        {
            this.Vdata();


            var sb = new StringBuilder();
            foreach (var item in this.SubData)
            {
                var format = string.Format("<input type=hidden name=\"{0}\" value=\"{1}\"/>", item.Key, item.Value);
                sb.Append(format);
            }

            var html = string.Format(@"<form action='{0}' method='{1}'  accept-charset='utf-8'>{2}</form><script>document.forms[0].submit();</script>", this.Gateway, this.Method(), sb);

            return html;
        }

        /// <summary>
        /// 发送url
        /// </summary>
        /// <returns></returns>
        public virtual string FasongHttp()
        {
            this.Vdata();
            
            var hh = new HttpHelper();

            var l = new List<string>();
            foreach (var item in this.SubData)
            {
                l.Add(item.Key + "=" + item.Value);
            }
           
            hh.Url = this.Gateway;
            hh.Data = string.Join("&", l);
            return hh.PostPage();
        }

        /// <summary>
        /// 验证数据合法
        /// </summary>
        private void Vdata()
        {
            if (this.SubData == null)
            {
                throw new Exception("请先使用SetData，写入数据");
            }

            if (string.IsNullOrWhiteSpace(this.Gateway))
            {
                throw new Exception("网关Gateway不能为空");
            }
        }

        /// <summary>
        /// 发送的数据(不含html)
        /// </summary>
        public Dictionary<string, string> SubData { get; protected set; }

        /// <summary>
        /// 写入data
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> SetData()
        {
            this.SubData = this.GetSubData();
            return this.SubData;
        }

        /// <summary>
        /// 写入data
        /// </summary>
        /// <returns></returns>
        protected abstract Dictionary<string, string> GetSubData();

    }

}